﻿namespace WebApplication1
{
    public class MyBackgroundService : BackgroundService//,IHostedService
    {
        private readonly Taskdispatch taskdispatch;
        public  MyBackgroundService(Taskdispatch taskdispatch)
        {
            this.taskdispatch = taskdispatch;
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(1000).ContinueWith(tsk =>
                {
                    taskdispatch.SchedulingTasks();
                });
            }        
        }

        public override  Task StartAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("任务启动....");
            return  base.StartAsync(cancellationToken);
             
        }

        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("任务停止....");
             await base.StopAsync(cancellationToken);
        }
    }
}
